﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using Newtonsoft.Json;
using System.Text;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Param.Group
{
    public partial class EditExp : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected SysParam _gourpDB;

        protected List<ParamGroupExp> _expList;

        protected int _expListIndex;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {
                ParamKit.Bind("ParamGroupExpType", ddl_type, null, null, null, "-1", null);
                ParamKit.Bind("TranferParam", ddl_relation, null, null, new List<string>() { "TranferParam" }, null, null);

                rp_typejs.DataSource = ParamKit.GetParamList("ParamGroupExpType", "IsRelation", null);
                rp_typejs.DataBind();

                tb_code.Text = _expList[_expListIndex].Code;
                tb_name.Text = _expList[_expListIndex].Name;
                ddl_type.SelectedValue = _expList[_expListIndex].Type;
                ddl_relation.SelectedValue = _expList[_expListIndex].Relation;

                if (ParamKit.GetParam("ParamGroupExpType", _expList[_expListIndex].Type, "IsRelation", "0") == "0")
                {
                    ddl_relation.Enabled = false;
                }
            }
        }

        protected void InitializeGlobalValue()
        {
            string groupId;

            try
            {
                if (!IsPostBack)
                {
                    groupId = Request["groupid"] == null ? "" : Request["groupid"];
                    _groupId.Text = groupId;

                    _code.Text = Request["code"] == null ? "" : Request["code"];
                }
                else
                {
                    groupId = _groupId.Text;
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _gourpDB = session.CreateCriteria<SysParam>().Add(Expression.Eq("GroupCode", "TranferParam")).Add(Expression.Eq("ItemCode", groupId)).UniqueResult<SysParam>();
                }

                if (_gourpDB == null)
                {
                    throw new Exception("找不到对应参数信息.");
                }

                _expList = JsonConvert.DeserializeObject<List<ParamGroupExp>>(_gourpDB.Param5);

                if (_expList == null)
                {
                    throw new Exception("找不到对应参数扩展信息.");
                }

                _expListIndex = _expList.FindIndex(FindCode);

                if (_expListIndex == -1)
                {
                    throw new Exception("找不到对应参数扩展项信息.");
                }

            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected bool FindCode(ParamGroupExp item)
        {
            if (item.Code == _code.Text)
            {
                return true;
            }
            return false;
        }

        protected void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _expList.RemoveAt(_expListIndex);

                    if (_expList.Exists(FindCode))
                    {
                        throw new Exception("该参数组已经存在.");
                    }

                    if (ParamKit.GetParam("ParamGroupExpType", ddl_type.SelectedValue, "IsRelation", "0") == "0")
                    {
                        _expList.Add(new ParamGroupExp(tb_code.Text, tb_name.Text, " ", ddl_type.SelectedValue));
                    }
                    else
                    {
                        _expList.Add(new ParamGroupExp(tb_code.Text, tb_name.Text, ddl_relation.SelectedValue, ddl_type.SelectedValue));
                    }

                    _gourpDB.Param5 = JsonConvert.SerializeObject(_expList);

                    session.Update(_gourpDB);
                    session.Flush();
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            // 刷新 editparamgroup 页面
            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent",
                navTabId = "editparamgroup"
            });
        }
    }
}
